<template>
  <div class="mul-func-select">
    <div class="show-title" @click="toggle">
      <label for="titleTips" v-if="selectedNum === 0">Please Select</label>
      <span id="titleTips" v-else>Selected quantity:{{selectedNum}}</span>
    </div>
    <!--加个clickoutside.js就行了-->
    <div class="show-content" v-show="showDrapContent">
      <transition enter-class="el-zoom-in-top-enter" enter-active-class="el-zoom-in-top-enter-active"
                  leave-class="el-zoom-in-top-leave" leave-active-class="el-zoom-in-top-leave-active"
                  leave-to-class="el-zoom-in-top-leave-to">
        <div class="wrap-div">
          <div class="filter-input">
            <el-input v-model="filterInputValue" placeholder="Filter"></el-input>
          </div>
          <el-scrollbar>
            <div class="content-item">
              <el-checkbox-group v-model="checkList">
                <el-checkbox style="display: block;margin: 0 20px;padding: 0;"  :label="item.value" v-for="item in checkData" :key="item.value"></el-checkbox>
              </el-checkbox-group>
            </div>
          </el-scrollbar>
          <div class="foot-sellect-all">
            <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
          </div>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        selectedNum: 0,
        filterInputValue: '',
        isIndeterminate: true,
        checkAll: false,
        checkList: [], // 选中的值
        checkData: [], // 总数据数组
        showDrapContent: false
      }
    },
    props: {
      propsData: Array
    },
    methods: {
      handleCheckAllChange(val) {
        this.checkList = val ? this.checkData.map(item=>item.value) : [];
        this.isIndeterminate = false;
      },
      toggle() {
        this.showDrapContent = ! this.showDrapContent
      }
    },
    watch: {
      'filterInputValue': function(value) {
        if (value !== '') {
          this.checkData = this.checkData.filter(v => {
            return v.label.toLowerCase().includes(value.toLowerCase())
          })
        } else {
          this.checkData = this.propsData
        }
      },
      'propsData': function(value) {
        console.log(value,999)
        // 单向数据流；父组件propsData数据流向子组件；每次父组件的改变就会重新穿给子组件自己的数据
        this.checkData = this.propsData
      },
      'checkList': function(value) {
        console.log(value, '选中的值')
        this.selectedNum = value.length
        this.$emit('input', value) // 为了实现v-model绑定
        this.$emit('change') // 为了实现在子组件上暴露change事件给父组件
      }
    },
    mounted() {
    },
  }
</script>

<style lang="scss" scoped>
.mul-func-select {
  .show-title {
    background-color: #fff;
    border: 1px solid #ccc;
    font-size: 14px;
    border-radius: 4px;
    box-sizing: border-box;
    width: 100%;
    height: 40px;
    line-height: 40px;
    cursor: pointer;
    color: #5a5e66;
    display: inline-block;
    outline: none;
    user-select: none;
    padding-left: 20px;
  }
  .show-content {
    margin-top: 10px;
    background-color: #fff;
    border: 1px solid #d1dbe5;
    border-radius: 4px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
    .wrap-div {
      .content-item {
        height: 250px;
        padding: 20px 10px;
        .el-checkbox {
          margin: 5px 20px!important;
        }
      }
      .foot-sellect-all {
        height: 40px;
        padding: 10px 0 10px 30px;
      }
    }
  }
}
</style>
